//
//  WATFFTVListViewController.swift
//  WiFiFaster
//
//  Created by one on 2024/1/5.
//

import UIKit

class WATFFTVListViewController: WATFBaseCollectionViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        watf_updateLocalData()
        WATFManager.watf_manager.watf_alertShow = false
        WATFManager.watf_manager.watf_resultBack = true
        
        NotificationCenter.default.addObserver(self, selector: #selector(watf_updateLocalData), name: watf_ftvLocalUpdate_noti, object: nil)
    }
    //MARK: - watf action
    @objc func watf_updateLocalData() {
        watf_ftvModels.removeAll()
     
     watf_ftvModels = WATFManager.watf_manager.watf_ftvModels
        if watf_ftvModels.count == 0 {
            if WATFManager.watf_manager.watf_smartFTVModel == nil {
                let watf_model = WATFManager.watf_manager.watf_createServerModel()
                watf_model.watf_select = true
                watf_ftvModels.append(watf_model)
            }else {
                let watf_model = WATFManager.watf_manager.watf_createServerModel()
                watf_ftvModels.append(watf_model)
            }
        }
       
        let watf_mc: CGFloat = CGFloat(watf_ftvModels.count)
        var watf_colH:CGFloat = 0
        if WATFManager.watf_manager.watf_smartFTVModel == nil && watf_ftvModels.count == 0{
            watf_colH = 0
        }
        if WATFManager.watf_manager.watf_smartFTVModel == nil && watf_ftvModels.count != 0{
            watf_colH = watf_mc * 54 + (watf_mc-1) * 12 + 20 + 54
        }
        if WATFManager.watf_manager.watf_smartFTVModel != nil {
            watf_colH = watf_mc * 54 + (watf_mc-1) * 12 + 54 * 3 + 20
        }
        if WATFManager.watf_manager.watf_smartFTVModel != nil && watf_ftvModels.count == 0{
            watf_colH = 54 * 2 + 20
        }
        let watf_maxH = (watf_SCREEN_HEIGHT-WATFCommon.watf_navHegiht-10-WATFCommon.watf_XBottomSpace-15)
        if watf_colH >= watf_maxH {
            watf_colH = watf_maxH
            watf_collectionView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 20, right: 0)
        }else {
            watf_collectionView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        }
        watf_collectionView.snp.updateConstraints { make in
            
            make.height.equalTo(watf_colH)
        }
        watf_collectionView.reloadData()
    }
    override func watf_layout() -> UICollectionViewFlowLayout {
        let watf_layout = UICollectionViewFlowLayout()
        watf_layout.itemSize = CGSize(width: watf_SCREEN_WIDTH-56, height: 54)
        watf_layout.minimumLineSpacing = 12
//        watf_layout.headerReferenceSize = CGSize(width: watf_SCREEN_WIDTH-56, height: 54)
        return watf_layout
    }
    override func watf_collectionViews() {
        
        super.watf_collectionViews()
        view.addSubview(watf_navView)
        watf_collectionView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.width.equalTo(watf_SCREEN_WIDTH-32)
            make.top.equalTo(watf_navView.snp.bottom).offset(10)
            make.height.equalTo(400)
            
        }
        watf_collectionView.backgroundColor = WATFCommon.watf_getHXColorValue(rgb: 0x322E2B)
        watf_collectionView.layer.cornerRadius = 16
        watf_collectionView.dataSource = self
        watf_collectionView.delegate = self
        watf_collectionView.register(WATFFTVListCell.classForCoder(), forCellWithReuseIdentifier: WATFFTVListCell.watf_viewToString)
        watf_collectionView.register(WATFFTVListHeadView.classForCoder(), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: WATFFTVListHeadView.watf_viewToString)
        
    }
    @objc func watf_smartClick() {
        WATFManager.watf_manager.watf_resultBack = false
        WATFManager.watf_manager.watf_smartFTVModel?.watf_select = true
        navigationController?.popViewController(animated: true)
        NotificationCenter.default.post(name: watf_ftvConnect_noti, object: nil)
    }
    //MARK: - watf lazy
    lazy var watf_navView: WATFNavView = {
        let watf_navView = WATFNavView()
        watf_navView.watf_titleLabel.text = "Servers"
        return watf_navView
    }()
    var watf_ftvModels = [WATFFTVTempModel]()
}
extension WATFFTVListViewController: UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 2
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if section == 0 {
            return WATFManager.watf_manager.watf_smartFTVModel != nil ? 1 : 0
        }
        return watf_ftvModels.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if indexPath.section == 0 {
            let watf_cell = collectionView.dequeueReusableCell(withReuseIdentifier: WATFFTVListCell.watf_viewToString, for: indexPath) as! WATFFTVListCell
            watf_cell.watf_ftvModel = WATFManager.watf_manager.watf_smartFTVModel
            return watf_cell
        }
        let watf_cell = collectionView.dequeueReusableCell(withReuseIdentifier: WATFFTVListCell.watf_viewToString, for: indexPath) as! WATFFTVListCell
        watf_cell.watf_ftvModel = watf_ftvModels[indexPath.row]
        return watf_cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let _ = watf_ftvModels.map { watf_model in
            return watf_model.watf_select = false
        }
        if indexPath.section == 0 {
            WATFManager.watf_manager.watf_smartFTVModel?.watf_select = true
        }else {
            WATFManager.watf_manager.watf_smartFTVModel?.watf_select = false
            let watf_ftvModel = watf_ftvModels[indexPath.row]
            watf_ftvModel.watf_select = true
        }
        WATFManager.watf_manager.watf_resultBack = false
        NotificationCenter.default.post(name: watf_ftvConnect_noti, object: nil)
        navigationController?.popViewController(animated: true)
    }
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        let watf_headView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: WATFFTVListHeadView.watf_viewToString, for: indexPath) as! WATFFTVListHeadView
        watf_headView.watf_titleLabel.text = indexPath.section == 0 ? "Smart location" : "Servers"
        return watf_headView
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        if WATFManager.watf_manager.watf_smartFTVModel == nil && section == 0{
            return .zero
        }
        return CGSize(width: watf_SCREEN_WIDTH-56, height: 54)
    }
}

